package evolution.operation.succession;

import evolution.individual.Individual;

public class BestSuccessionStrategy implements SuccessionStrategy {

	@Override
	public Individual[] success(Individual[] parents, Individual[] offspring) {
		offspring[findWorst(offspring)] = parents[findBest(parents)];
		return offspring;
	}

	private static int findBest(Individual[] individuals) {
		int max = 0;
		for (int i = 0; i < individuals.length; i++) {
			if (individuals[i].compareTo(individuals[max]) > 0) {
				max = i;
			}
		}
		return max;
	}

	private static int findWorst(Individual[] individuals) {
		int min = 0;
		for (int i = 0; i < individuals.length; i++) {
			if (individuals[i].compareTo(individuals[min]) < 0) {
				min = i;
			}
		}
		return min;
	}
}
